
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Koperasi;
import database.DBClass;
import java.util.List;
import validation.ValidateKoperasi;

/**
 *
 * @author Jagadeesh
 */
//@WebServlet(name = "Controller",
//        loadOnStartup = 1,
//        urlPatterns = {"/insertproduk",
//    "/viewproduk",
//    "/insertkoperasi",
//    "/viewkoperasi"
//})
public class ControllerServletk extends HttpServlet {

    //declare values to get form values from jsp page 
    String idkoperasi;
    String namakoperasi;
    String alamatkoperasi;
    String deskripsikoperasi;
    String notelpkoperasi;
    String actionk;
    String kotakoperasi;
    Koperasi koperasi = new Koperasi();
    ValidateKoperasi validatekoperasi = new ValidateKoperasi();
    DBClass dbobject = new DBClass();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //get the values from jsp page
            idkoperasi = request.getParameter("idkoperasi");
            namakoperasi = request.getParameter("namakoperasi");
            alamatkoperasi = request.getParameter("alamatkoperasi");
            deskripsikoperasi = request.getParameter("deskripsikoperasi");
            notelpkoperasi = request.getParameter("notelpkoperasi");
            kotakoperasi = request.getParameter("kotakoperasi");
            actionk = request.getParameter("actionk");
            System.out.println(idkoperasi);
            
            if (actionk.equals("Insert Koperasi")) {
                //set values to koperasi.For this call below method
                setValuesTokoperasi();

                //check all form values are valid or not. send koperasi object 
                Koperasi checkedkoperasi = validatekoperasi.validateData(koperasi);
                System.out.println(checkedkoperasi.getIsValid());
                System.out.println(koperasi.getnamakoperasi());
                if (!checkedkoperasi.getIsValid()) {

                    //if data is invalid.set koperasi object in request and pass that request to
                    //insertupdate.jsp using forward
                    checkedkoperasi.setActionk("Insert Koperasi");
                    request.setAttribute("error", checkedkoperasi);

                    RequestDispatcher rd = request.getRequestDispatcher("admin/insertupdatekoperasi.jsp");
                    rd.forward(request, response);
                    //now display errors in that jsp page
                } else {
                    //using DBClass object call insertDetails method and pass koperasi object
                    dbobject.insertKoperasi(koperasi);
                    List list = dbobject.getAllkoperasi();
                    request.setAttribute("list", list);
                    //forward to insertupdate page using requestdispatcher
                    RequestDispatcher rd = request.getRequestDispatcher("admin/viewkoperasi.jsp");
                    //display a message to client.store message in request object
                    //forwarding to jsp
                    rd.forward(request, response);


                }
            }
            if (actionk.equals("editkoperasi")) {
                //get userdetails of particular name
                Koperasi ukoperasi = dbobject.getKoperasi(idkoperasi);
                request.setAttribute("updateuser", ukoperasi);
                RequestDispatcher rd = request.getRequestDispatcher("admin/insertupdatekoperasi.jsp");
                rd.forward(request, response);
            }
            if (actionk.equals("Update Koperasi")) {


                setValuesTokoperasi();
                Koperasi checkedkoperasi = validatekoperasi.validateData(koperasi);
                System.out.println(checkedkoperasi.getIsValid());
                if (!checkedkoperasi.getIsValid()) {

                    //if data is invalid.set koperasi object in request and pass that request to
                    //insertupdate.jsp using forward
                    checkedkoperasi.setActionk("Update Koperasi");
                    request.setAttribute("error", checkedkoperasi);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/insertupdatekoperasi.jsp");
                    rd.forward(request, response);
                    //now display errors in that jsp page
                } else {
                    //using DBClass object call insertDetails method and pass koperasi object
                    System.out.println(idkoperasi);
                    dbobject.UpdateKoperasi(koperasi, idkoperasi);
                    
                    List list = dbobject.getAllkoperasi();
                    request.setAttribute("list", list);
                    //forward to insertupdate page using requestdispatcher
                    RequestDispatcher rd = request.getRequestDispatcher("admin/viewkoperasi.jsp");
                    //display a message to client.store message in request object
                    //forwarding to jsp
                    rd.forward(request, response);


                }

            }
            if (actionk.equals("deletekoperasi")) {
                //delete userdetails of particular name
                dbobject.deletekoperasi(idkoperasi);
                List list = dbobject.getAllkoperasi();
                request.setAttribute("list", list);
                RequestDispatcher rd = request.getRequestDispatcher("admin/viewkoperasi.jsp");
                rd.forward(request, response);
            }
            if (actionk.equals("detailkoperasi")) {
                request.setAttribute("linkproduk",idkoperasi);
                Koperasi ukoperasi = dbobject.getKoperasi(idkoperasi);
                request.setAttribute("detailkoperasi", ukoperasi);
                RequestDispatcher rd = request.getRequestDispatcher("koperasidetails.jsp");
                rd.forward(request, response);
            }
        } catch (Exception e) {
            out.println(e);
            System.out.println(e);
        } finally {
            out.close();
        }
    }
    //this method is used to setvalues to koperasi


    public void setValuesTokoperasi() {
        koperasi.setnamakoperasi(namakoperasi);
        koperasi.setalamatkoperasi(alamatkoperasi);
        koperasi.setdeskripsikoperasi(deskripsikoperasi);
        koperasi.setnotelpkoperasi(notelpkoperasi);
        koperasi.setKota(kotakoperasi);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    public String getServletInfo() {
        return "Short description";
    }
}
